package com.courseselector.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.courseselector.common.Result;
import com.courseselector.entity.course.Course;
import com.courseselector.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程管理控制器
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/course")
public class CourseController {
    
    @Autowired
    private CourseService courseService;
    
    @ApiOperation("分页查询课程列表")
    @GetMapping("/list")
    public Result<Page<Course>> list(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页记录数") @RequestParam(defaultValue = "10") Integer size) {
        Page<Course> pageParam = new Page<>(page, size);
        Page<Course> pageResult = courseService.getCourseList(pageParam);
        return Result.success(pageResult);
    }
    
    @ApiOperation("根据ID获取课程信息")
    @GetMapping("/{id}")
    public Result<Course> getById(@ApiParam("课程ID") @PathVariable Integer id) {
        Course course = courseService.getCourseById(id);
        if (course == null) {
            return Result.fail("课程不存在");
        }
        return Result.success(course);
    }
    
    @ApiOperation("添加课程")
    @PostMapping
    public Result<Boolean> add(@ApiParam("课程信息") @RequestBody Course course) {
        boolean result = courseService.addCourse(course);
        return result ? Result.success() : Result.fail("添加课程失败");
    }
    
    @ApiOperation("更新课程信息")
    @PutMapping
    public Result<Boolean> update(@ApiParam("课程信息") @RequestBody Course course) {
        boolean result = courseService.updateCourse(course);
        return result ? Result.success() : Result.fail("更新课程信息失败");
    }
    
    @ApiOperation("删除课程")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@ApiParam("课程ID") @PathVariable Integer id) {
        boolean result = courseService.deleteCourse(id);
        return result ? Result.success() : Result.fail("删除课程失败");
    }
} 